package com.list88zhu.android.sunflower;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.list88zhu.android.sunflower.api.ZhihuApi;
import com.list88zhu.android.sunflower.bean.NewsLatestBean;
import com.list88zhu.android.sunflower.http.AppClient;
import com.list88zhu.android.sunflower.jetpack.lifecycle.Presenter;

import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * <pre><code>
 *
 *  </code></pre>
 *
 * @author zhuyingming@qmtv.com
 * @date 2018/9/8
 */
public class RetrofitActivity extends AppCompatActivity {

    private static final String TAG = "RetrofitActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrifit);
        findViewById(R.id.get).setOnClickListener(view -> {

            AppClient.create(ZhihuApi.class).getNewsLatest()
                .subscribeOn(Schedulers.io())
                .subscribe(newsLatestBean -> {
                    Log.d(TAG, newsLatestBean.toString());
                });
        });

        getLifecycle().addObserver(new Presenter());
    }



    private void get() {
        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
        clientBuilder.retryOnConnectionFailure(true);

        Retrofit retrofit = new Retrofit.Builder()
            .client(clientBuilder.build())
            .baseUrl("https://news-at.zhihu.com")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

        retrofit.create(ZhihuApi.class).getNewsLatest()
            .subscribeOn(Schedulers.io())
            .subscribe(newsLatestBean -> {
                Log.d(TAG, newsLatestBean.toString());
            });
    }
}
